$(function (){
    let app = new Vue({
        el: "#app",
        data: {
            sysTitle: "固定资源管理系统",
            userID: "", //用户ID
            userName: "", //用户姓名
            userImageSrc: "", //用户头像
            userClass: "",  //用户类型
            userDepartment: "", //用户类型
            backImageSrc: "images/common/back.jpg",
            subList1: [
                {href:"properties.html", li: " 资源管理"},
                {href:"departments.html", li: " 部门管理"},
                {href:"employees.html", li: " 员工管理"}
            ],
            subList2: [
                {href:"infos.html", li: " 个人信息"},
                {href:"passwords.html", li: " 密码修改"},
                {href:"util/login.html", li: " 注销"}
            ],
            departments: [],
            dataList: [],//当前页要展示的列表数据
            dialogFormVisible: false,//添加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            formData: {},//编辑表单数据
            formDataAdd: {},//添加表单数据
            formDepart: {},
            employeeHeadPhoto: "",
            rules: {//校验规则
                password: [{ required: true, message: '密码(>=8位)为必填项', trigger: 'blur' }],
                manger: [{ required: true, message: '是否为主管为必选项', trigger: 'blur' }],
                id: [{ required: true, message: '员工编号为必填项', trigger: 'blur' }],
                did: [{ required: true, message: '员工部门为必选项', trigger: 'blur' }],
                name: [{ required: true, message: '员工姓名为必填项', trigger: 'blur' }],
                tell: [{ required: true, message: '员工电话(11位)为必填项', trigger: 'blur' }],
                time: [{ required: true, message: '注册时间为必填项', trigger: 'blur' }],
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:8,//每页显示的记录数
                total:0,//总记录数
                id: "",
                name: "",
                selectDepartment: "",
            }
        },
        created(){
            this.getIdAndName();
        },
        methods: {
            getIdAndName(){
                if(sessionStorage.getItem("userID") === null)
                {
                    location.href = "util/login.html";
                }else {
                    this.userID = sessionStorage.getItem("userID").toString();
                    this.userName = sessionStorage.getItem("userName").toString();
                    this.userImageSrc = "../" + sessionStorage.getItem("userImageSrc").toString();
                    this.userClass = sessionStorage.getItem("userClass").toString();
                    this.userDepartment = sessionStorage.getItem("userDepartment").toString();
                    this.userType = sessionStorage.getItem("userType").toString();
                    //数据显示
                    this.getAll();
                }
            },
            //分页查询
            getAll() {
                 //组织参数，拼接url请求地址
                 // console.log(this.pagination.type);
                 param ="?name="+this.pagination.name;
                 param +="&id="+this.pagination.id;
                 param +="&did="+this.pagination.selectDepartment;
                 //发送异步请求
                 axios.get("http://localhost:16/employees/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param).then((res)=>{
                     this.pagination.pageSize = res.data.data.size;
                     this.pagination.currentPage = res.data.data.current;
                     this.pagination.total = res.data.data.total;
                     this.dataList = res.data.data.records;
                     console.log(this.dataList);
                     for(var i = 0 ; i<this.dataList.length; i++)
                     {
                         this.dataList[i].time = this.dataList[i].time.toString().slice(0, 10);
                     }
                 });
                //部门数据源绑定
                this.getDataSource();
            },

            //部门数据源绑定
            getDataSource() {
                //为部门select绑定数据源
                axios.get("http://localhost:16/departments").then((res)=>{
                    this.departments = res.data.data;
                });
            },

            //切换页码
            handleCurrentChange(currentPage) {
                //修改页码值为当前选中的页码值
                this.pagination.currentPage = currentPage;
                //执行查询
                this.getAll();
            },

            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
                this.formDataAdd.password = "12345678";
                this.formDataAdd.image = "head.jpg";
                this.employeeHeadPhoto = "../images/upload/" + this.formDataAdd.image;
            },

            //重置表单
            resetForm() {
                this.formDataAdd = {};
            },

            //添加
            handleAdd () {
                axios.get("http://localhost:16/employees/"+this.formDataAdd.id).then((res)=>{
                    if(res.data.flag)
                    {
                        this.$message.error("系统中已存在该员工编号！")
                    }else {
                        axios.get("http://localhost:16/departments/"+this.formDataAdd.did).then((res)=>{
                            this.formDepart = res.data.data;
                        });
                        axios.post("http://localhost:16/employees", this.formDataAdd).then((res)=>{
                            if(res.data.flag){
                                this.formDepart.number = this.formDepart.number + 1;
                                if(this.formDataAdd.manger === "是"){
                                    if(this.formDepart.eid !== null){
                                        axios.get("http://localhost:16/employees/"+this.formDepart.eid).then((res)=>{
                                            if (res.data.flag)
                                            {
                                                let employeeMa = res.data.data;
                                                employeeMa.manger = "否";
                                                axios.put("http://localhost:16/employees",employeeMa).finally(()=>{
                                                    this.getAll();
                                                });
                                            }
                                        });
                                    }
                                    this.formDepart.eid = this.formDataAdd.id;
                                }

                                axios.put("http://localhost:16/departments", this.formDepart).finally(()=>{
                                    this.getAll();
                                });
                                //关闭弹窗
                                this.dialogFormVisible = false;
                                this.$message.success(res.data.msg);
                            }else
                                this.$message.error(res.data.msg);
                        }).finally(()=>{
                            //重新加载数据
                            this.getAll();
                        });
                    }
                });
            },

            //取消
            cancel(){
                this.dialogFormVisible = false;
                this.dialogFormVisible4Edit = false;
                this.$message.info("操作已取消");
            },

            // 删除
            handleDelete(row) {
               this.$confirm("是否要删除"+row.name+"员工的信息", "删除提示", {type:"info"}).then(()=>{
                   axios.get("http://localhost:16/departments/"+row.did).then((res)=>{
                       this.formDepart = res.data.data;
                   });
                   axios.delete("http://localhost:16/employees/"+row.id).then((res)=>{
                       if(res.data.flag){
                           this.$message.success(res.data.msg);
                           this.formDepart.number = this.formDepart.number - 1;
                           axios.put("http://localhost:16/departments", this.formDepart).finally(()=>{
                               this.getAll();
                           });
                       }else
                           this.$message.error(res.data.msg);
                   }).finally(()=>{
                       //重新加载数据
                       this.getAll();
                   });
               }).catch(()=>{
                   this.$message.info("取消操作");
               });
            },

            //弹出编辑窗口
            handleUpdate(row) {
                axios.get("http://localhost:16/employees/"+row.id).then((res)=>{
                    if(res.data.flag && res.data.data != null)
                    {
                        this.dialogFormVisible4Edit = true;
                        this.formData = res.data.data;
                        axios.get("http://localhost:16/departments/"+this.formData.did).then((res)=>{
                            this.formDepart = res.data.data;
                        });
                        this.employeeHeadPhoto = "../images/upload/" + this.formData.image;
                   }else {
                       this.$message.error("数据同步失败，自动刷新");
                   }
               }).finally(()=>{
                   this.getAll();
               });
            },

            //修改
            handleEdit() {
              if(this.formData.name === "" || this.formData.password === "" || this.formData.tell === ""){
                  this.$message.error("修改信息不能为空！");
                  return;
              }
              axios.put("http://localhost:16/employees", this.formData).then((res)=>{
                  if(res.data.flag)
                  {
                      if(this.formData.manger === "是"){
                          if(this.formDepart.eid !== null )
                          {
                              if(this.formDepart.eid !== this.formData.id)
                              {
                                  axios.get("http://localhost:16/employees/"+this.formDepart.eid).then((res)=>{
                                      if (res.data.flag)
                                      {
                                          let employeeMa = res.data.data;
                                          employeeMa.manger = "否";
                                          axios.put("http://localhost:16/employees",employeeMa).finally(()=>{
                                              this.getAll();
                                          });
                                      }
                                  });
                              }
                          }
                          this.formDepart.eid = this.formData.id;
                      }
                      axios.put("http://localhost:16/departments", this.formDepart).finally(()=>{
                          this.getAll();
                      });
                      this.dialogFormVisible4Edit = false;
                      this.$message.success(res.data.msg);
                  }else {
                      this.$message.error(res.data.msg);
                  }
              }).finally(()=>{
                  this.getAll();
              });
            },

        }
    });
    var left_nav = $(".left-nav");
    var left_lis = left_nav.children("li");
    var left_sub_nav = left_lis[0].children[1];
    $(left_sub_nav).children("li").eq(2).children("a").addClass("current");
   /* lis.addClass("current");*/
});